﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 酒店管理
{
    public partial class 界面 : Form
    {
        private int curOperator;//当前操作员编号
        private int curOpPower;//当前操作员权限
        private string curRoom;//当前对象房间编号
        private string curCust;//当前对象客户编号
        private string curBill = null;//当前处理账单号
        DataSet tmpDS;
        database tmpDB;
        选择客人 selectForm;//当有客人合住时提供选择客人菜单

        string sharenum;//当前房间登记合住数(当前指向该房的登记入住笔数)

        public static string s;
        public 界面()
        {
            InitializeComponent();
            tmpDS = new DataSet();  //创建通用数据集
            tmpDB = new database();  //实例化数据库
            tmpDB.DataSetInit(ref tmpDS);
        }

        private void button8_Click(object sender, EventArgs e)//房间按钮点击事件
        {
            
            this.roomBox.Text = ((Button)sender).Text;//焦点房号改变
            s = ((Button)sender).Text;
            curRoom = ((Button)sender).Text;//程序临时变量获得当前房号
            //在入住单中查找对应客户号
            string sql="select count(账单号) from 登记入住 where 房间号='" + curRoom + "' and 预订标记=0";
            tmpDB.SelectCMD(sql, ref tmpDS);
            DataTable tmpdt = tmpDS.Tables["Buffer"];
            sharenum = tmpdt.Rows[0][0].ToString();
            //MessageBox.Show("sharenum: "+sharenum+" !");
            if("0" == sharenum)curCust = null;
            if ("1" == sharenum)
            {
                //MessageBox.Show(curRoom);
                sql = "select 客户编号 from 登记入住 where 房间号='" + curRoom + "' and 预订标记=0";
                tmpDB.SelectCMD(sql, ref tmpDS);
                tmpdt = tmpDS.Tables["Buffer"];
                curCust = tmpdt.Rows[0][0].ToString();
                //MessageBox.Show(curCust);
            }
            if ("1" != sharenum && "0" != sharenum)
            {
                ;//合住房则先不选择客人对象，打开功能时才调用selectCustInRoom()进行选择
            }
            MessageBox.Show("房间号： " + curRoom + "\n当前住客： " + curCust);
            
        }

        private void 界面_Load(object sender, EventArgs e)
        {
            //读数据库
        }

        private void 客人预定YToolStripMenuItem_Click(object sender, EventArgs e)
        {
            客人预定 form = new 客人预定();
            form.ShowDialog();
        }

        private void 续房交费LToolStripMenuItem_Click(object sender, EventArgs e)
        {
            续房交费 form = new 续房交费();
            form.ShowDialog();
        }

        private void 消费ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            客人消费登记 form = new 客人消费登记();
            form.ShowDialog();
        }

        private void 客人登记YToolStripMenuItem_Click(object sender, EventArgs e)
        {
            客人登记 form = new 客人登记();
            form.ShowDialog();
        }

        private void 修改等级ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            登记信息修改 form = new 登记信息修改();
            form.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Button btn in this.tabControl3.TabPages[4].Controls)
            {
                if (btn.BackColor!=Color.Red)
                {
                    btn.Visible = false;
                }
            }
        }

        public bool selectCustInRoom()//检查合住情况，如有合住则需通过客人选择界面选择其一
        {
            //检查房间中的客户数
            tmpDB.SelectCMD("select COUNT(*) from 登记入住 where 房号='" + curRoom + "' and 预订标记=0", ref tmpDS);
            DataTable tmpdt = tmpDS.Tables["Buffer"];
            sharenum = tmpdt.Rows[0][0].ToString();
            if ("0" == sharenum)
            {
                curCust = null;
                return true;
            }
            if ("1" == sharenum)
            {
                /*
                string sql = "select * from 登记入住";
                tmpDB.SelectCMD(sql, ref tmpDS);
                MessageBox.Show(tmpDS.Tables["Buffer"].Rows[1][0].ToString());
                */

                tmpDB.SelectCMD("select 客户编号 from 登记入住 where 房号='" + curRoom + "' and 预订标记=0", ref tmpDS);
                tmpdt = tmpDS.Tables["Buffer"];
                curCust = tmpdt.Rows[0][0].ToString();
                return false;
            }
            if ("1" != sharenum && "0" != sharenum)
            {
                //新建选择客人界面，并通过选择获取客人号
                selectForm = new 选择客人(curRoom, ref tmpDS, ref tmpDB, sharenum);
                selectForm.button1.Click += new EventHandler(selectFormButton);
                return false;
            }
            MessageBox.Show("判断房间人数时出错");
            return false;
        }

        public void selectFormButton(object sender, EventArgs e)//检查合住情况，如有合住则需通过客人选择界面选择其一
        {
            curCust = selectForm.returnsCust();
        }

        private void 置空净房CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //同步数据库获得房间信息
            tmpDB.SelectCMD("select 房间状态 from 房间 where 房号='"+curRoom+"'",ref tmpDS);
            DataTable tmpdt = tmpDS.Tables["Buffer"];
            string curStats = tmpdt.Rows[0][0].ToString();
            if ("脏房" == curStats)//检查当前房间是否为脏房
            {
                button8.BackColor = System.Drawing.Color.LawnGreen;

                tmpDB.UpdateCMD("update 房间 set 房间状态='空净房' where 房号='" + curRoom + "'", ref tmpDS);//修改数据库中房间状态操作

                MessageBox.Show("已置空净房！");
            }
            else MessageBox.Show("该房不是脏房，不能置空净房！");
        }

        private void 更换房间LToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.selectCustInRoom())
            {
                MessageBox.Show("此房间并无住户!");
                return;
            }
            更换房间 form = new 更换房间(curRoom, curCust, curBill, ref tmpDS, ref tmpDB);
            form.ShowDialog();
        }

        private void 界面_Activated(object sender, EventArgs e)
        {
            //从子窗体回到主窗体则更新DATASET，刷新房状态
        }

        private void 删除客单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string sql;
            double prePay;//预付款
            int buyNum;//该用户消费单的消费笔数
            string remain;//检测删除后房间剩下的人数
            string trBill, trManNum, trRT, trLT;//对应登记入住 房号&客号 的　账单号\登记人数\抵达时间\离开时间
            if (this.selectCustInRoom())
            {
                MessageBox.Show("此房间并无住户!");
                return;
            }

            //检查登记入住中其对应信息预收款>0则提示不可
            sql = "select 预收款 from 登记入住 where 房间号='" + curRoom + "' and 客户编号='"+curCust+"'";
            tmpDB.SelectCMD(sql, ref tmpDS);
            DataTable tmpdt = tmpDS.Tables["Buffer"];
            prePay = Convert.ToDouble(tmpdt.Rows[0][0].ToString());
            if (0 == prePay)
            {
                MessageBox.Show("该客户已预付款，无法退单\n请结账退房");
                return;
            }
            //检查房间消费中存在　房号&客号　数!="0" 则提示不可
            sql = "select count(*) from 房间消费 where 消费房间号='" + curRoom + "' and 消费客户号='"+curCust+"'";
            tmpDB.SelectCMD(sql, ref tmpDS);
            tmpdt = tmpDS.Tables["Buffer"];
            buyNum = Convert.ToInt16(tmpdt.Rows[0][0].ToString());
            if (0 != buyNum)
            {
                MessageBox.Show("该客户已有消费项目，无法退单\n请结账退房");
                return;
            }
            //查找对应　登记入住 房号&客号 的　账单号\登记人数\抵达时间\离开时间
            sql = "select 账单号,登记人数,抵达时间,离开时间 from 登记入住 where 房间号='" + curRoom + "' and 客户编号='" + curCust + "'";
            tmpDB.SelectCMD(sql, ref tmpDS);
            tmpdt = tmpDS.Tables["Buffer"];
            trBill = tmpdt.Rows[0][0].ToString();
            trManNum = tmpdt.Rows[0][1].ToString();
            trRT = tmpdt.Rows[0][2].ToString();
            trLT = tmpdt.Rows[0][3].ToString();

            //插入账单 "insert into 账单(账单号,房号,客户号,登记人数,抵达时间,离开时间,客户付款数,备注 values(''','"+curRoom+"','"+curCust+"',)"
            sql = "insert into 账单(账单号,房号,客户号,登记人数,抵达时间,离开时间,客户付款数,备注) values('" + trBill + "','" + curRoom + "','" + curCust + "','" + trManNum + "','" + trRT + "',SYSTIME,0,'删除客单')";
            tmpDB.InsertCMD(sql, ref tmpDS);
            //删除登记入住信息
            sql = "delete * from 登记入住 where 房间号='" + curRoom + "' and 客户编号='"+curCust+"'";
            tmpDB.DeleteCMD(sql, ref tmpDS);

            //改变房间状态,删后空则设脏
                //更改房间状态,考虑是否未住满房
            sql = "select sum(*) from 登记入住 where 房间号='" + curRoom + "'";
            tmpDB.SelectCMD(sql, ref tmpDS);
            tmpdt = tmpDS.Tables["Buffer"];
            remain = tmpdt.Rows[0][0].ToString();//读出该房间剩余客人数
            if ("0" != remain) return;
            sql = "update 房间 set 房间状态='脏房' where 房间号='" + curRoom + "'";
            tmpDB.UpdateCMD(sql, ref tmpDS);
        }

    }
}